package com.igoal.utils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.igoal.dto.GoalDTO;

public class WebUtils {
	public static final int DEFAULT_MAX_GOAL_CLOUD_RANGE = 8;
	/**
	 * Assume the goal list is sorted by userCount
	 * @param goals
	 * @return list of {@link GoalCloudItem}
	 */
	public static Set<GoalDTO> buildGoalCloud(List<GoalDTO> goals){
		if(goals == null) return null;
		
		return buildGoalCloud(goals, DEFAULT_MAX_GOAL_CLOUD_RANGE);
	}
	
	public static Set<GoalDTO> buildGoalCloud(List<GoalDTO> goals, int maxRange){
		if(maxRange <= 0) maxRange = DEFAULT_MAX_GOAL_CLOUD_RANGE;
		
		Set<GoalDTO> items = new HashSet<GoalDTO>();
		int maxUserCount = 0;
		float weight = 1f;
		
		for(GoalDTO goal : goals){
			if(goal.getUserCount() > maxUserCount){
				maxUserCount = goal.getUserCount();
			}
		}
		
		if(maxUserCount > 0)
			weight = (float)maxRange / (float)maxUserCount;
		
		for(GoalDTO goal : goals){
			int cloudIndex = (int) (goal.getUserCount() * weight);
			if(cloudIndex == 0){
				cloudIndex = 0 + (int)(Math.random()*4);
			}
			goal.setCloudIndex(cloudIndex);
			items.add(goal);
		}
		
		return items;
	}
}
